﻿/*
 * 文件名称：
 * 程序员：wangyun
 * 创建时间：2008/05/27
 * 
 * 功能说明：功能模块的字段类型控件，多选框控件
 *          多选CheckBox的值会以"|"分隔开,数据库里的存储方式为:|1|2|3|4|,类推
 * 
 * 修改人员：
 *          yylu 2008/12/23 增加多选CheckBoxList,修改单选每行显示数的赋值
 *          yylu 2009/08/04 增加三级联动
 *          yylu 2009/08/20 增加控件手写单独使用的代码
 *          yhe  2009/09/14 增加动态绑定类别的选择，根据不同的规则绑定不同的类别
 * 
 * 修改日期：
 * 
 * 修改记录：
 */

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Whir.Common.WebHelper;

public partial class UserControls_FieldControl_ListBoxType : Whir.Controls.ModelField.BaseFieldControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Whir.BLL.Whir_FieldOption bll = new Whir.BLL.Whir_FieldOption();
        int fieldTextId = int.Parse(bll.GetList(" FieldID = " + this.FieldID).Tables[0].Rows[0][0].ToString());
        Whir.Model.Whir_FieldOption model = bll.GetModel(fieldTextId);

        if (this.isSingleUse)
        {
            this.InitFieldControl(this.FieldID);
        }

        if (!IsPostBack)
        {
            BindData(model);
        }
        else
        {
            if (model.optionType == 1)//下拉
            {
                this.FieldValue = this.DrpList.SelectedValue;
            }
            else if (model.optionType == 2)//单选
            {
                this.FieldValue = this.RadlList.SelectedValue;
            }
            else if (model.optionType == 3)//多选
            {
                this.FieldValue = getChkBoxValue();
            }
            else if(model.optionType == 4)//二级联动
            {
                if (DropDownList3.Visible == true && DropDownList3.SelectedItem.Value!="-1")
                {
                    this.FieldValue = DropDownList3.SelectedItem.Value;
                }
                else if (DropDownList2.Visible == true && DropDownList2.SelectedItem.Value != "-1")
                {
                    this.FieldValue = DropDownList2.SelectedItem.Value;
                }
                else if (DropDownList1.Visible == true && DropDownList1.SelectedItem.Value != "-1")
                {
                    this.FieldValue = DropDownList1.SelectedItem.Value;
                }
            }
        }
    }

    private void BindData(Whir.Model.Whir_FieldOption model)
    {
        int ItemID = NetRequest.GetQueryInt("ItemID");
        //选项显示类型（1、下拉列表；2、单选按钮;3.多选）
        if (model.optionType == 1)
        {
            this.PlhDropList.Visible = true;
        }
        else if (model.optionType == 2)
        {
            this.PlhRadioList.Visible = true;
            this.RadlList.RepeatColumns = model.cellNum;
        }
        else if (model.optionType == 3)
        {
            this.PlhCheckBoxList.Visible = true;
            this.ChBoxList.RepeatColumns = model.cellNum;
        }
        else if (model.optionType == 4)
        {
            this.PlhDropListAjax.Visible = true;
        }

        //选项绑定类型（1、绑定文本；2、绑定SQL）
        if (model.bindType == 1)
        {
            string s = model.bindValue;
            if (!string.IsNullOrEmpty(s))
            {
                string[] items = s.Split(',');
                string itemText;
                string itemValue;
                foreach (string item in items)
                {
                    itemText = item.Split('|')[0].ToString();
                    itemValue = item.Split('|')[1].ToString();

                    if (model.optionType == 1)
                        this.DrpList.Items.Add(new ListItem(itemText, itemValue));
                    else if (model.optionType == 2)
                        this.RadlList.Items.Add(new ListItem(itemText, itemValue));
                    else if (model.optionType == 3)
                        this.ChBoxList.Items.Add(new ListItem(itemText, itemValue));
                }
            }
        }
        else if (model.bindType == 2)
        {
            if (model.optionType == 1)
            {
                this.DrpList.DataSource = Whir.SQLServerDAL.DbHelperSQL.Query(model.sql);
                this.DrpList.DataTextField = model.textField;
                this.DrpList.DataValueField = model.valueField;
                this.DrpList.DataBind();

                //更新操作，绑定值
                this.DrpList.SelectedValue = FieldCommon.GetBindValue(this.FieldID, ItemID);
                //if(ItemID==137)
                DrpList.Items.Insert(0, new ListItem("= 请选择 =", "-1"));


              //  string JsStr = string.Format("insert(document.all('{0}').options[document.all('{0}').selectedIndex].value);", DrpList.ClientID);
                string JsStr = "eWebEditor1.setHTML(this.value)"; 
                DrpList.Attributes.Add("onchange", JsStr);

            }
            else if (model.optionType == 2)
            {
                this.RadlList.DataSource = Whir.SQLServerDAL.DbHelperSQL.Query(model.sql);
                this.RadlList.DataTextField = model.textField;
                this.RadlList.DataValueField = model.valueField;
                this.RadlList.DataBind();

                this.RadlList.SelectedValue = FieldCommon.GetBindValue(this.FieldID, ItemID);
            }
            else if (model.optionType == 3)
            {
                this.ChBoxList.DataSource = Whir.SQLServerDAL.DbHelperSQL.Query(model.sql);
                this.ChBoxList.DataTextField = model.textField;
                this.ChBoxList.DataValueField = model.valueField;
                this.ChBoxList.DataBind();

                FieldValue = FieldCommon.GetBindValue(this.FieldID, ItemID);
                setChkBoxValue();
            }

           
        }
        else if (model.bindType == 3)
        {
            this.DrpList.DataTextField = model.textField;
            this.DrpList.DataValueField = model.valueField;
            Whir.Common.BindHelper.BindDropDownList(this.DrpList, model.sql, model.sqlChild, "", "=== 请选择 ===");

            //更新操作，绑定值
            this.DrpList.SelectedValue = FieldCommon.GetBindValue(this.FieldID, ItemID);
        }
        else if (model.bindType == 4)
        {
            string strSql = "SELECT " + model.textField + "," + model.valueField + " FROM " + model.sql + " WHERE ParentID=" + model.sqlChild;

            this.DropDownList1.DataSource = Whir.SQLServerDAL.DbHelperSQL.Query(strSql);
            this.DropDownList1.DataTextField = model.textField;
            this.DropDownList1.DataValueField = model.valueField;
            this.DropDownList1.DataBind();

            DropDownList1.Items.Insert(0, new ListItem("= 请选择 =", "-1"));

            string value = FieldCommon.GetBindValue(this.FieldID, ItemID);

            if (string.IsNullOrEmpty(value))
            {
                DropDownList2.Visible = false;
                DropDownList3.Visible = false;
            }
            else
            {
                if (DropDownList1.Items.FindByValue(value) != null)
                {
                    DropDownList1.SelectedValue = value;
                    DropDownList2.Visible = false;
                    DropDownList3.Visible = false;
                }
                else
                {
                    string parentValue = model.sqlChild;
                    strSql = "SELECT ParentID FROM " + model.sql + " WHERE " + model.valueField + " = (SELECT ParentID FROM " + model.sql + " WHERE " + model.valueField + " = " + value + ")";
                    parentValue = Whir.SQLServerDAL.DbHelperSQL.GetSingle(strSql).ToString();

                    if (parentValue == model.sqlChild)
                    {
                        strSql = "SELECT ParentID FROM " + model.sql + " WHERE " + model.valueField + " = " + value;
                        parentValue = Whir.SQLServerDAL.DbHelperSQL.GetSingle(strSql).ToString();

                        DropDownList1.SelectedValue = parentValue;
                        BindDDL(DropDownList2, parentValue);

                        DropDownList2.SelectedValue = value;
                        DropDownList3.Visible = false;
                    }
                    else
                    {
                        DropDownList1.SelectedValue = parentValue;
                        BindDDL(DropDownList2, parentValue);

                        strSql = "SELECT ParentID FROM " + model.sql + " WHERE " + model.valueField + " = " + value;
                        parentValue = Whir.SQLServerDAL.DbHelperSQL.GetSingle(strSql).ToString();
                        DropDownList2.SelectedValue = parentValue;

                        BindDDL(DropDownList3, parentValue);
                        DropDownList3.SelectedValue = value;
                    }

                }
            }

            
        }
        else if (model.bindType == 5)
        {
            string strWhere = model.sqlChild; //获得当前条修的
            string[] strTemps = strWhere.Replace("\r\n", "|").Split('|');
            int TypeID = NetRequest.GetQueryInt("TypeID"); //获取当前栏目编号
            string sqlChild = "";  //当前栏目对应的类别父类

            //获取当前类别所对应的规则类别
            for (int i = 0; i < strTemps.Length; i++)
            {
                if (strTemps[i].Trim() != string.Empty)
                {
                    string strTypeID = strTemps[i].Split('=')[0];
                    if (strTypeID != string.Empty && strTypeID == TypeID.ToString())
                    {
                        sqlChild = strTemps[i].Split('=')[1];
                        break;
                    }
                }
            }
            
            //该栏目对应的规则存在
            if (sqlChild != string.Empty)
            {
                this.DrpList.DataTextField = model.textField;
                this.DrpList.DataValueField = model.valueField;
                Whir.Common.BindHelper.BindDropDownList(this.DrpList, model.sql, sqlChild, "", "=== 请选择 ===");

                //更新操作，绑定值
                this.DrpList.SelectedValue = FieldCommon.GetBindValue(this.FieldID, ItemID);
            }
        }

        //if (!string.IsNullOrEmpty(model.defaultValue))
        //{
        if (ItemID == 0)
        {
            //添加操作，绑定默认值
            this.DrpList.SelectedValue = model.defaultValue;
            this.RadlList.SelectedValue = model.defaultValue;
            //this.ChBoxList.SelectedValue = model.defaultValue;
        }
        else
        {
            //更新操作，绑定值
            this.DrpList.SelectedValue = FieldCommon.GetBindValue(this.FieldID, ItemID);
            this.RadlList.SelectedValue = FieldCommon.GetBindValue(this.FieldID, ItemID);
            FieldValue = FieldCommon.GetBindValue(this.FieldID, ItemID);
            setChkBoxValue();
        }
        //}
    }

    /// <summary>
    /// 设置ChkBoxList的值.设置多选的值
    /// </summary>
    /// <returns></returns>
    private void setChkBoxValue()
    {
        for (int k = 0; k < ChBoxList.Items.Count; k++)
        {
            if (FieldValue.IndexOf("|" + ChBoxList.Items[k].Value + "|") >= 0)
            {
                ChBoxList.Items[k].Selected = true;
            }
        }
    }

    /// <summary>
    /// 获取ChkBoxList的值.获取多选的值
    /// </summary>
    /// <returns></returns>
    private string getChkBoxValue()
    {
        string strReturn = "|";
        foreach (ListItem item in ChBoxList.Items)
        {
            if (item.Selected)
            {
                strReturn += item.Value + "|";
            }
        }
        return strReturn;
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDDL(this.DropDownList2, this.DropDownList1.SelectedValue);
    }

    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDDL(this.DropDownList3, this.DropDownList2.SelectedValue);
    }

    //绑定下拉列表
    protected void BindDDL(DropDownList ddl, string pid)
    {
        ddl.Items.Clear();
        ddl.Items.Add(new ListItem("= 请选择 =", "-1"));

        Whir.BLL.Whir_FieldOption bll = new Whir.BLL.Whir_FieldOption();
        int fieldTextId = int.Parse(bll.GetList(" FieldID = " + this.FieldID).Tables[0].Rows[0][0].ToString());
        Whir.Model.Whir_FieldOption model = bll.GetModel(fieldTextId);

        string strSql = "SELECT " + model.textField + "," + model.valueField + " FROM " + model.sql + " WHERE ParentID=" + pid;

        DataTable dt = Whir.SQLServerDAL.DbHelperSQL.Query(strSql).Tables[0];

        if (dt.Rows.Count > 0)
        {

            foreach (DataRow dr in dt.Rows)
            {
                ddl.Items.Add(new ListItem(dr[model.textField].ToString(), dr[model.valueField].ToString()));
            }
            ddl.Visible = true;
        }
        else
        {
            ddl.Visible = false;
        }
    }
}
